package Server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class HWTestThread extends Thread {
	
	private PhoneServer server;
	private List<PhoneServerThread> phoneList;
	
	public HWTestThread(PhoneServer s, List<PhoneServerThread> l) {
    	super("HWTestThread");
    	server = s;
    	phoneList = l;
    }
    
    public void run() {
    	
    	List<PhoneServerThread> failedPhones = new ArrayList<PhoneServerThread>(); 
    	
    	for (PhoneServerThread p : phoneList) {
    		if (!p.hardwareTest()) {
    			failedPhones.add(p);
    			PhoneServerDB.getPhoneServerDB().updatePhoneStatus(p.getPhoneName(),"Crashed");
    		} else {
    			PhoneServerDB.getPhoneServerDB().updatePhoneStatus(p.getPhoneName(), "Running");
    		}
    	}
    	
    	if (!failedPhones.isEmpty()) {
	    	for (PhoneServerThread f : failedPhones) {
	    		phoneList.remove(f);
	    		String subject = "Phone Failure";
	    		String body = "Phone IP " + f.getPhoneName() + " is not responding.";		
	    		Date date = new Date();
	    		String viewed = "false";
	    		PhoneServerDB.getPhoneServerDB().insertAdminMessage(subject, body, date, viewed);
	    	}
    	}
    	
    }

}
